package org.example.algorithm.stack;

import java.util.Stack;

/**
 * 最小栈:
 * 请你设计一个 最小栈 。它提供 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈。
 */
//通过两个栈实现
public class MinStackSolution {
    private Stack<Integer> normalStack;
    private Stack<Integer> minStack;

    public static void main(String[] args) {
    }

    /** initialize your data structure here. */
    public MinStackSolution() {
        normalStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x) {
        normalStack.push(x);
        if (minStack.isEmpty() || minStack.peek() >= x) {
            minStack.push(x);
        }
    }

    public void pop() {
        int min = normalStack.pop();
        if (min == minStack.peek()) {
            minStack.pop();
        }

    }

    public int top() {
        return normalStack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
